package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiDianPing;
import cn.com.api.entity.ApiDingDan;
import cn.com.api.mapper.ApiDianPingMapper;
import cn.com.api.mapper.ApiDingDanMapper;
import cn.com.api.requestpage.DianPingPage;
import cn.com.api.resultpage.CarOrderPage;
import cn.com.api.resultpage.DianpingPage;
import cn.com.api.resultpage.InitDianPingPage;
import cn.com.api.resultpage.InitShopDianPingPage;
import cn.com.api.service.ApiCarService;
import cn.com.api.service.ApiDianPingService;
import cn.com.util.*;

@Service
public class ApiDianPingServiceImpl implements ApiDianPingService {
	@Autowired
	private ApiCarService apiCarService;
	@Autowired
	private ApiDianPingMapper dianPingMapper;
	@Autowired
	private ApiDingDanMapper apiDingDanMapper;

	@Override
	public ApiDianPing searchDianPingByCid(int c_id) {
		return dianPingMapper.searchDianPingByCid(c_id);
	}

	@Override
	public List<ApiDianPing> searchDianPingBySid(int c_sid) {
		List<ApiDianPing> list = dianPingMapper.searchDianPingBySid(c_sid);
		return list;
	}

	@Override
	public void searchDianPingByGoodsid(Map<String, String> params, Map<String, Object> map) {
		Long goods_id = Long.valueOf(params.get("goods_id"));
		Integer pageCount = Integer.parseInt(params.get("pageCount"));
		Integer type = Integer.parseInt(params.get("type"));

		PageInfo page = new PageInfo();
		page.setCurrentPage(pageCount);
		/**
		 * type : 0-全部 1-满意 2-不满意
		 */
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("goods_id", goods_id);
		if (0 == type) {

		} else if (1 == type) {
			paramsMap.put("type", 1);
		} else if (2 == type) {
			paramsMap.put("type", 2);
		}
		List<DianpingPage> dianPingList = dianPingMapper.searchDianPingByGoodsidAndGradeListPage(paramsMap, page);
		InitDianPingPage initDianPingPage = new InitDianPingPage();
		initDianPingPage.setDianPingList(dianPingList);
		initDianPingPage.setTotalPage(page.getTotalPage());

		map.put("datas", initDianPingPage);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "success");

	}

	@Override
	public Map<String, Object> searchDianPingCountRateByGoodsid(Long c_goodsid) {
		Map<String, Object> map = dianPingMapper.searchDianPingCountRateByGoodsid(c_goodsid);
		return map;
	}

	@Override
	public void addDianPingToOrder(DianPingPage dianPingPage) {
		String u_id = dianPingPage.getU_id();// 用户id
		String titles = dianPingPage.getTitles();// 订单号
		Integer c_dtype = dianPingPage.getC_dtype();// 商品质量-几颗星
		Integer is_raise = dianPingPage.getIs_raise();// 是否点赞
		String c_text = dianPingPage.getC_text();// 点评内容
		// 根据订单号查询订单
		ApiDingDan dingDan = apiDingDanMapper.searchDingDanByTitleId(titles);
		if (dingDan != null) {
			String tel = dingDan.getTel();
			String[] goodsIdList = tel.split(",");
			HashSet<String> goodsIdSet = new HashSet<String>();
			if (goodsIdList != null && goodsIdList.length > 0) {
				for (String goodsId : goodsIdList) {
					goodsIdSet.add(goodsId);
				}
			}
			// 根据订单号查询是否存在评价----存在则更新，不存在则插入
			List<ApiDianPing> dianPingList = dianPingMapper.searchDianPingByOrderid(dingDan.getTitles());
			if (dianPingList != null && dianPingList.size() > 0) {// 已点评过（对配送的点评则先插入，这是更新表）
				for (ApiDianPing dianPing : dianPingList) {
					dianPing.setC_userid(u_id);
					dianPing.setC_orderid(dingDan.getTitles());
					dianPing.setC_sid(dingDan.getShop_id());
					dianPing.setC_dtype(c_dtype);
					// dianPing.setC_goodsid(goods_id);
					dianPing.setIs_raise(is_raise);
					dianPing.setC_text(c_text);
					dianPingMapper.updateDianPing(dianPing);
				}
			} else {// 未点评过，则插入表
				if (goodsIdList != null && goodsIdList.length > 0) {
					for (String goodsid : goodsIdSet) {
						Long goods_id = Long.parseLong(goodsid);
						ApiDianPing dianPing = new ApiDianPing();
						dianPing.setC_userid(u_id);
						dianPing.setC_orderid(dingDan.getTitles());
						dianPing.setC_sid(dingDan.getShop_id());
						dianPing.setC_dtype(c_dtype);
						dianPing.setC_goodsid(goods_id);
						dianPing.setIs_raise(is_raise);
						dianPing.setC_text(c_text);
						dianPing.setC_appdate(DateUtil.nowTimeToString());
						dianPingMapper.addDianPing(dianPing);
					}
				}

			}

		}

	}

	/** 商品评价信息 评分，总数 满意数 不满意数量 */
	@Override
	public void searchDianPingSumInfoByGoodsid(Map<String, String> params, Map<String, Object> map) {
		Long goods_id = Long.valueOf(params.get("goods_id"));

		InitDianPingPage initDianPingPage = new InitDianPingPage();

		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("goods_id", goods_id);
		Map<String, Object> results = dianPingMapper.searchDianPingSumInfoByGoodsid(paramsMap);
		Integer allCount = Integer.parseInt(results.get("count").toString());
		Integer sumScore = Integer.parseInt(results.get("score").toString());

		initDianPingPage.setAllCount(allCount);

		// type=0 全部 1-满意 2-不满意
		paramsMap.put("type", 1);
		results = dianPingMapper.searchDianPingSumInfoByGoodsid(paramsMap);
		Integer goodCount = Integer.parseInt(results.get("count").toString());
		initDianPingPage.setGoodCount(goodCount);

		paramsMap.put("type", 2);
		results = dianPingMapper.searchDianPingSumInfoByGoodsid(paramsMap);
		Integer badCount = Integer.parseInt(results.get("count").toString());
		initDianPingPage.setBadCount(badCount);

		double score = 0.0;
		double goodRate = 0.0;
		if (allCount != 0) {
			score = sumScore / (float) allCount;
			goodRate = goodCount / (float) allCount;
		}
		initDianPingPage.setScore(score);
		initDianPingPage.setGoodRate(goodRate);

		map.put("datas", initDianPingPage);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "success");

	}

	/**
	 * 商品评价信息 评分，一星-数量(*星:数量)
	 */
	@Override
	public void searchDianPingSumInfoByShopid(Map<String, String> params, Map<String, Object> map) {
		Integer shop_id = Integer.parseInt(params.get("shop_id"));

		InitShopDianPingPage initShopDianPingPage = new InitShopDianPingPage();

		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("shop_id", shop_id);

		List<Map<String, Object>> results = dianPingMapper.searchDianPingSumInfoByShopid(paramsMap);
		/** *星-数量 */
		Integer oneCount = 0;
		Integer twoCount = 0;
		Integer threeCount = 0;
		Integer fourCount = 0;
		Integer fiveCount = 0;
		Integer allCount = 0;
		Integer sumScore = 0;
		if (results != null && results.size() > 0) {
			for (Map<String, Object> maps : results) {
				Integer grade = Integer.parseInt(maps.get("grade").toString());
				Integer count = Integer.parseInt(maps.get("count").toString());
				if (1 == grade) {
					oneCount = count;
				}
				if (2 == grade) {
					twoCount = count;
				}
				if (3 == grade) {
					threeCount = count;
				}
				if (4 == grade) {
					fourCount = count;
				}
				if (5 == grade) {
					fiveCount = count;
				}
				allCount = allCount + count;
				sumScore = sumScore + grade * count;
			}
		}

		initShopDianPingPage.setAllCount(allCount);
		initShopDianPingPage.setOneCount(oneCount);
		initShopDianPingPage.setTwoCount(twoCount);
		initShopDianPingPage.setThreeCount(threeCount);
		initShopDianPingPage.setFourCount(fourCount);
		initShopDianPingPage.setFiveCount(fiveCount);

		Double score = 0.0;
		if (allCount != 0) {
			score = sumScore / (double) allCount;// 评分
		}
		initShopDianPingPage.setScore(score);

		/** 未回复差评，未回复 type :1-未回复差评 2-未回复 0-全部 */
		Integer noReplyBadCount = 0;
		paramsMap.put("type", 1);// 1-未回复的差评
		results = dianPingMapper.searchDianPingSumInfoByShopid(paramsMap);
		if (results != null && results.size() > 0) {
			for (Map<String, Object> maps : results) {
				Integer count = Integer.parseInt(maps.get("count").toString());
				noReplyBadCount = noReplyBadCount + count;
			}
		}

		Integer noReplyCount = 0;
		paramsMap.put("type", 2);// 2-未回复
		results = dianPingMapper.searchDianPingSumInfoByShopid(paramsMap);
		if (results != null && results.size() > 0) {
			for (Map<String, Object> maps : results) {
				Integer count = Integer.parseInt(maps.get("count").toString());
				noReplyCount = noReplyCount + count;
			}
		}

		/**
		 * 客户端查询 好评率： 全部 满意的 不满意的
		 */
		Integer goodCount = threeCount + fourCount + fiveCount;
		Integer badCount = oneCount + twoCount;
		Double goodRate = 0.0;
		if (allCount != 0) {
			goodRate = goodCount / (double) allCount;
		}

		initShopDianPingPage.setGoodCount(goodCount);
		initShopDianPingPage.setBadCount(badCount);
		initShopDianPingPage.setGoodRate(goodRate);

		initShopDianPingPage.setNoReplyBadCount(noReplyBadCount);
		initShopDianPingPage.setNoReplyCount(noReplyCount);

		map.put("datas", initShopDianPingPage);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "success");
	}

	@Override
	public void searchDianPingByShopid(Map<String, String> params, Map<String, Object> map) {
		Integer shop_id = Integer.parseInt(params.get("shop_id"));
		Integer pageCount = Integer.parseInt(params.get("pageCount"));
		Integer type = Integer.parseInt(params.get("type"));

		PageInfo page = new PageInfo();
		page.setCurrentPage(pageCount);
		/**
		 * type : 0-全部 1-未回复的差评 2-未回复 3-好评 4-差评
		 */
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("shop_id", shop_id);
		if (0 == type) {

		} else if (1 == type) {
			paramsMap.put("type", 1);
		} else if (2 == type) {
			paramsMap.put("type", 2);
		} else if (3 == type) {
			paramsMap.put("type", 3);
		} else if (4 == type) {
			paramsMap.put("type", 4);
		}
		List<DianpingPage> dianPingList = new ArrayList<DianpingPage>();
		dianPingList = dianPingMapper.searchDianPingByShopidAndGradeListPage(paramsMap, page);
		if (dianPingList != null && dianPingList.size() > 0) {
			for (DianpingPage dianPing : dianPingList) {
				String car_ids = dianPing.getCar_ids();
				List<CarOrderPage> productList = new ArrayList<CarOrderPage>();
				productList = apiCarService.searchCarProductsByCids(car_ids);
				dianPing.setProductList(productList);
			}
		}

		InitShopDianPingPage initShopDianPingPage = new InitShopDianPingPage();
		initShopDianPingPage.setDianPingList(dianPingList);
		initShopDianPingPage.setTotalPage(page.getTotalPage());
		map.put("datas", initShopDianPingPage);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "success");
	}

	/** 商铺的所有评价 */
	public void searchAllDianPingByShopid(Map<String, String> params, Map<String, Object> map) {
		Integer shop_id = Integer.parseInt(params.get("shop_id"));
		Integer pageCount = Integer.parseInt(params.get("pageCount"));
		Integer type = Integer.parseInt(params.get("type"));

		PageInfo page = new PageInfo();
		page.setCurrentPage(pageCount);
		/**
		 * type : 0-全部 1-未回复的差评 2-未回复 3-好评 4-差评
		 */
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("shop_id", shop_id);
		if (0 == type) {

		} else if (1 == type) {
			paramsMap.put("type", 1);
		} else if (2 == type) {
			paramsMap.put("type", 2);
		} else if (3 == type) {
			paramsMap.put("type", 3);
		} else if (4 == type) {
			paramsMap.put("type", 4);
		}
		List<DianpingPage> dianPingList = new ArrayList<DianpingPage>();
		dianPingList = dianPingMapper.searchAllDianPingByShopidAndGradeListPage(paramsMap, page);
		if (dianPingList != null && dianPingList.size() > 0) {
			for (DianpingPage dianPing : dianPingList) {
				String car_ids = dianPing.getCar_ids();
				List<CarOrderPage> productList = new ArrayList<CarOrderPage>();
				productList = apiCarService.searchCarProductsByCids(car_ids);
				dianPing.setProductList(productList);
			}
		}

		InitShopDianPingPage initShopDianPingPage = new InitShopDianPingPage();
		initShopDianPingPage.setDianPingList(dianPingList);
		initShopDianPingPage.setTotalPage(page.getTotalPage());
		map.put("datas", initShopDianPingPage);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "success");
	}

	@Override
	public void replyOrderEvaluate(Map<String, String> params, Map<String, Object> map) {
		String orderId = params.get("orderId");
		String content = params.get("content");
		Date nowDate = new Date();
		// 根据订单号--查询评价列表---回复评论--更新
		List<ApiDianPing> dianPingList = dianPingMapper.searchDianPingByOrderid(orderId);

		if (dianPingList != null && dianPingList.size() > 0) {
			for (ApiDianPing dianPing : dianPingList) {
				dianPing.setC_redate(DateUtil.nowTimeToString());
				dianPing.setC_retext(content);
				dianPing.setIs_reply(1);// 已恢复
				dianPingMapper.updateDianPing(dianPing);
			}
		}
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "回复成功！");
	}

	public Integer getDianPingSize(HashMap map) {
		return dianPingMapper.getDianPingSize(map);
	}

	public Integer getAllDianPingSize(HashMap map) {
		return dianPingMapper.getAllDianPingSize(map);
	}

	public void replyDianPing(ApiDianPing apiDianPing) {
		dianPingMapper.replyDianPing(apiDianPing);
	}

	@Override
	public void addDianPing(ApiDianPing apiDianPing) {
		// TODO Auto-generated method stub
		dianPingMapper.addDianPing(apiDianPing);

	}

	@Override
	public void updDianPing(ApiDianPing apiDianPing) {
		dianPingMapper.updateDianPing(apiDianPing);

	}

	@Override
	public ApiDianPing getDianPingByCondition(String orderNum, String productId) {
		// TODO Auto-generated method stub
		return dianPingMapper.getDianPingByCondition(orderNum, productId);
	}

	@Override
	public List<Map<String, Object>> getDianPingPageByGoodId(int goodId, PageInfo pageInfo) {
		// TODO Auto-generated method stub
		return dianPingMapper.getGoodDianPingListPage(goodId, pageInfo);
	}

	@Override
	public void batchInsert(List<ApiDianPing> list) {
		dianPingMapper.batchInsert(list);

	}

	@Override
	public List<Map<String, Object>> selectOrderDianPing(Map<String, Object> map, PageInfo pageInfo) {
		return dianPingMapper.selectOrderDianPingListPage(map, pageInfo);
	}

	@Override
	public void logicDelDianPing(String dpId) {
		// TODO Auto-generated method stub
		dianPingMapper.logicDelDianPing(dpId);

	}

}
